<template>
  <div class="center">
    <!-- <h1>欢迎来到学习系统</h1> -->
    <div class="logon">
      <div :class="overlaylong">
        <div class="overlaylong-Signin" v-if="disfiex == 0">
          <h2 class="overlaylongH2">登录</h2>
          <el-form
            size="mini"
            ref="loginForm"
            :model="loginForm"
            :rules="loginRules"
            auto-complete="on"
            label-position="top"
          >
            <el-form-item prop="username">
              <el-input
                size="medium"
                ref="username"
                v-model="loginForm.username"
                placeholder="请输入用户名"
                name="username"
                type="text"
                tabindex="1"
                auto-complete="on"
              />
            </el-form-item>

            <el-form-item prop="password">
              <el-input
                size="medium"
                :key="passwordType"
                ref="password"
                v-model="loginForm.password"
                :type="passwordType"
                placeholder="请输入密码"
                name="password"
                tabindex="2"
                auto-complete="on"
                @keyup.enter.native="handleLogin"
              />
            </el-form-item>

            <el-form-item prop="captcha">
              <div class="verify">
                <el-input
                  size="medium"
                  ref="captcha"
                  v-model="loginForm.code"
                  placeholder="请输入验证码"
                  name="captcha"
                  type="text"
                  tabindex="1"
                  auto-complete="on"
                />

                <div class="verify_imgbox"  @click="chageCaptcha()">
                  <img  :src="captcha.image" />
                </div>
                <!-- <el-button @click="chageCaptcha()" type="text" class="textbtn"
                  >看不清，换一张</el-button
                > -->
              </div>
            </el-form-item>
          </el-form>
          <button class="inupbutton" @click="handleLogin">登录</button>
        </div>
        <div class="overlaylong-Signup" v-if="disfiex == 1">
          <h2 class="overlaylongH2">注册</h2>
          <input type="text" placeholder="user" />
          <input type="text" placeholder="password" />
          <button class="inupbutton">注册</button>
        </div>
      </div>
      <div :class="overlaytitle">
        <div class="overlaytitle-Signin" v-if="disfiex == 0">
          <h2 class="overlaytitleH2">还没有加入我们？</h2>
          <p class="overlaytitleP">创建专属你的世界吧！！！</p>
          <div class="buttongohs" @click="Signin">加入世界</div>
        </div>
        <div class="overlaytitle-Signup" v-if="disfiex == 1">
          <h2 class="overlaytitleH2">已经加入我们啦？</h2>
          <p class="overlaytitleP">进入专属你的世界看看吧！！！</p>
          <div class="buttongohs" @click="Signup">进入世界</div>
        </div>
      </div>
    </div>
  </div>

  <!-- <div class="login-container">
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">

      <div class="title-container">
        <h3 class="title">Login Form</h3>
      </div>

      <el-form-item prop="username">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          ref="username"
          v-model="loginForm.username"
          placeholder="请输入用户名"
          name="username"
          type="text"
          tabindex="1"
          auto-complete="on"
        />
      </el-form-item>

      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          :key="passwordType"
          ref="password"
          v-model="loginForm.password"
          :type="passwordType"
          placeholder="请输入密码"
          name="password"
          tabindex="2"
          auto-complete="on"
          @keyup.enter.native="handleLogin"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
        </span>
      </el-form-item>
      <el-row>
        <el-col :span="12">
        <el-form-item prop="captcha">
          <span class="svg-container">
            <svg-icon icon-class="password"/>
          </span>
          <el-input
            size="mini"
            ref="captcha"
            v-model="loginForm.code"
            placeholder="请输入验证码"
            name="captcha"
            type="text"
            tabindex="1"
            auto-complete="on"
          />
        </el-form-item>
      </el-col>
     
      <el-col :span="12">

        <img style="width: 150px; height: 50px" :src="captcha.image" @click="chageCaptcha()"/>
        
      </el-col>
      
      </el-row>
      <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @click.native.prevent="handleLogin">登录</el-button>

    </el-form>
  </div> -->
</template>

<script>
import { validUsername } from "@/utils/validate";
import { captcha } from "@/api/user";
export default {
  name: "Login",
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error("Please enter the correct user name"));
      } else {
        callback();
      }
    };
    const validatePassword = (rule, value, callback) => {
      if (value.length < 5) {
        callback(new Error("The password can not be less than 6 digits"));
      } else {
        callback();
      }
    };
    const validateCaptcha = (rule, value, callback) => {
      if (this.loginForm.code.length < 1) {
        callback(new Error("请输入验证码"));
      } else if (this.loginForm.code != this.captcha.code) {
        callback(new Error("验证码错误"));
        this.chageCaptcha();
      } else {
        callback();
      }
    };

    return {
      overlaylong: "overlaylong",
      overlaytitle: "overlaytitle",
      disfiex: 0,
      loginForm: {
        username: "",
        password: "",
        code: "",
      },
      captcha: {
        code: "",
        image: "",
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", validator: validateUsername },
        ],
        password: [
          { required: true, trigger: "blur", validator: validatePassword },
        ],
        captcha: [
          { required: true, trigger: "blur", validator: validateCaptcha },
        ],
      },
      loading: false,
      passwordType: "password",
      redirect: undefined,
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true,
    },
  },
  methods: {
    chageCaptcha() {
      captcha().then((response) => {
        this.captcha.code = response.data.code;
        this.captcha.image = "data:image/png;base64," + response.data.image;
      });
    },
    showPwd() {
      if (this.passwordType === "password") {
        this.passwordType = "";
      } else {
        this.passwordType = "password";
      }
      this.$nextTick(() => {
        this.$refs.password.focus();
      });
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          const obj = {
            username: this.loginForm.username,
            password: this.loginForm.password,
          };
          this.$store
            .dispatch("user/login", obj)
            .then(() => {
              this.$router.push({ path: this.redirect || "/" });
              this.loading = false;
            })
            .catch(() => {
              this.loading = false;
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    Signin() {
      this.overlaylong = "overlaylongleft";
      this.overlaytitle = "overlaytitleright";
      setTimeout(() => {
        this.disfiex = 1;
      }, 200);
    },
    Signup() {
      this.overlaylong = "overlaylongright";
      this.overlaytitle = "overlaytitleleft";

      setTimeout(() => {
        this.disfiex = 0;
      }, 200);
    },
  },
  mounted() {
    captcha().then((response) => {
      this.captcha.code = response.data.code;
      this.captcha.image = "data:image/png;base64," + response.data.image;
    });
  },
};
</script>

<style scoped lang="scss">
.center {
  width: 100%;
  height: 100%;
  background-image: url("https://gd-hbimg.huaban.com/9165dc3c0f8279ae5402d1f47212847bff68b49f3cb547-QEli8R");
  background-size: 100% 100%;
  background-repeat: no-repeat;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
}

h1 {
  font-size: 30px;
  color: black;
}

.logon {
  border-radius: 10px;
  box-shadow: 0 14px 28px rgba(0, 0, 0, 0.25), 0 10px 10px rgba(0, 0, 0, 0.22);
  /* position: relative;
		overflow: hidden; */
  width: 50%;
  max-width: 100%;
  min-height: 60%;
  margin-top: 20px;
  display: flex;
  background: -webkit-linear-gradient(right, #4284db, #29eac4);
}

.overlaylong {
  border-radius: 10px 0 0 10px;
  width: 50%;
  height: 100%;
  background-color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
}

.overlaylongleft {
  border-radius: 0px 10px 10px 0px;
  width: 50%;
  height: 100%;
  background-color: #fff;
  transform: translateX(100%);
  transition: transform 0.6s ease-in-out;
  display: flex;
  align-items: center;
  justify-content: center;
}

.overlaylongright {
  border-radius: 10px 0 0 10px;
  width: 50%;
  height: 100%;
  background-color: #fff;
  transform: translateX(0%);
  transition: transform 0.6s ease-in-out;
  display: flex;
  align-items: center;
  justify-content: center;
}

.overlaytitle {
  border-radius: 0px 10px 10px 0px;
  width: 50%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0);
  display: flex;
  align-items: center;
  justify-content: center;
}

.overlaytitleH2 {
  font-size: 30px;
  color: #fff;
  margin-top: 20px;
}

.overlaytitleP {
  font-size: 15px;
  color: #fff;
  margin-top: 20px;
}

.overlaytitleleft {
  border-radius: 0px 10px 10px 0px;
  width: 50%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0);
  display: flex;
  align-items: center;
  justify-content: center;
  transform: translateX(0%);
  transition: transform 0.6s ease-in-out;
}

.overlaytitleright {
  border-radius: 0px 10px 10px 0px;
  width: 50%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0);
  display: flex;
  align-items: center;
  justify-content: center;
  transform: translateX(-100%);
  transition: transform 0.6s ease-in-out;
}

.overlaytitle-Signin {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
}

.overlaytitle-Signup {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
}

.buttongohs {
  width: 180px;
  height: 40px;
  border-radius: 50px;
  border: 1px solid #fff;
  color: #fff;
  font-size: 15px;
  text-align: center;
  line-height: 40px;
  margin-top: 40px;
}

.overlaylongH2 {
  font-size: 25px;
  color: black;
  /* width: 250px; */
}

.overlaylong-Signin {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column; 
  .verify{
     display: flex;
     .verify_imgbox{
      width: 120px;
      margin-left: 10px;
      height: 36px;
      img{
        width: 100%;
        height: 100%;
      }
    }
  }
}
 
   
.overlaylong-Signup {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
}

input {
  background-color: #eee;
  border: none;
  padding: 12px 15px;
  margin: 10px 0;
  width: 240px;
}
h3 {
  font-size: 10px;
  margin-top: 10px;
  cursor: pointer;
}
.inupbutton {
  background-color: #29eac4;
  border: none;
  width: 180px;
  height: 40px;
  border-radius: 50px;
  font-size: 15px;
  color: #fff;
  text-align: center;
  line-height: 40px;
  margin-top: 30px;
}
</style>
